﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using NEWS.Model;
namespace NEWS.DAL
{
    public class User
    {
        public static bool add(NEWS.Model.t_user user)
        {
            var db = new PetaPoco.Database("net");
            int userid = (int)db.Insert(user);
            if (userid > 0)
                return true;
            else
                return false;
        }

        public static bool loginorm(string username, string password)
        {
            var db = new PetaPoco.Database("net");
            var user = db.FirstOrDefault<NEWS.Model.t_user>("select * from t_user where username=@0 and password=@1", username, password);

            return user != null;
        }

        public static List<NEWS.Model.t_user> list()
        {
            var db = new PetaPoco.Database("net");
            return db.Query<NEWS.Model.t_user>("select * from t_user").ToList();
        }


        public static bool del(int id)
        {
            var db = new PetaPoco.Database("net");
            int i = db.Delete(id);
            if (i > 0)

                return true;
            else
                return false;
        }

        public static bool update(NEWS.Model.t_user user)
        {
            var db = new PetaPoco.Database("net");
            int i= db.Update(user);
            if (i > 0)

                return true;
            else
                return false;
        }


        public static NEWS.Model.t_user Getuser(int id)
        {
            var db = new PetaPoco.Database("net");
            return db.SingleOrDefault<NEWS.Model.t_user>("WHERE id=@0", id);

        }

        public static bool Search(string username)
        {
           
            var db = new PetaPoco.Database("net");
            var user = db.SingleOrDefault<t_user>("select * from t_user where username=@0", username);
            if (user == null)
                return true;
            else
                return false;


        }

    }
}
